package com.navi.loaddata.loadService.impl.Dcdata.cell1;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mesEntity.SpcDcdata;
import com.navi.loaddata.core.oracleEntity.EtlPdsGlassV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author jay
 */
@Service
public class ProcessDcDataServicePCELL1 {

    private static final Logger logger = LoggerFactory.getLogger(ProcessDcDataServicePCELL1.class);
    @Autowired
    private MesRepositoryImpl mesdao;
    public EtlPdsGlassV processDcData(SpcDcdata d) throws Exception {
        EtlPdsGlassV etlPdsGlassV = new EtlPdsGlassV();
        try {
            if ("A".equals(d.getMaterialname().substring(0,1))) {
                etlPdsGlassV.setComponentType("1");
            }else if("C".equals(d.getMaterialname().substring(0,1))) {
                etlPdsGlassV.setComponentType("4");
            }else{
                etlPdsGlassV.setComponentType("10");
            }
            etlPdsGlassV.setStepId(d.getProcessoperationname());
            etlPdsGlassV.setSubEquipId(d.getUnitname());
            etlPdsGlassV.setComponentId(d.getMaterialname());
            etlPdsGlassV.setGlassId(d.getMaterialname().substring(0,10));
            etlPdsGlassV.setGlassEndTime(d.getEventtime());
            etlPdsGlassV.setProductId(d.getProductspecname());
            etlPdsGlassV.setRouteId(d.getProcessflowname());
//            etlPdsGlassV.setGroupId(d.getLotname());
            etlPdsGlassV.setLotId(d.getLotname());
            etlPdsGlassV.setEquipId(d.getMachinename());
            etlPdsGlassV.setRunMode(d.getRunmode());
            if (StringUtils.isEmpty(d.getRecipename())) {
                etlPdsGlassV.setRecipeId(d.getProductspecname());
            } else {
                etlPdsGlassV.setRecipeId(d.getRecipename());
            }
            etlPdsGlassV.setOperatorId(d.getEventuser());
            etlPdsGlassV.setOwnerId(d.getOwnercode());
            etlPdsGlassV.setMesLinkKry(d.getMeslinkkey());

            if ("A".equals(d.getMaterialname().substring(0,1))) {
                etlPdsGlassV.setArrayGlassId(d.getMaterialname());
                List<String> cfGlassId=mesdao.findBySQL("SELECT CFPRODUCTNAME FROM mes_product WHERE 1=1 and factoryname='OC' and productname ='"+d.getMaterialname()+"'");
                if(!cfGlassId.isEmpty()) etlPdsGlassV.setCfGlassId(cfGlassId.get(0));
            }else if("C".equals(d.getMaterialname().substring(0,1).substring(0,1))){
                etlPdsGlassV.setCfGlassId(d.getMaterialname());
                List<String> arrayGlassId=mesdao.findBySQL("SELECT DestinationProductName FROM mes_product WHERE 1=1 and factoryname='OC' and productname ='"+d.getMaterialname()+"'");
                if(!arrayGlassId.isEmpty()) etlPdsGlassV.setArrayGlassId(arrayGlassId.get(0));
            }

        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCESS_MES2ORA_PDS_GLASS,e);
        }
//        etlPdsGlassV.setTaShift(); todo
        return etlPdsGlassV;
    }

}
